package ExpressTwo;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

public class OptimalReplacement extends JDialog implements WindowListener {
    private final MainInterface _D;
    private JTable table;
    private final int BlockNumber;//块数
    private final String[] name = {"次数", "页号", "顺序","缺页?"};
    private final int[] pages;//存储页号

    OptimalReplacement(MainInterface d) {
        _D = d;
        BlockNumber = _D.blockNumber;

        pages = new int[d.list_opt.getLength()];

        for (int i = 0; i < d.list_opt.getLength(); i++) {
            pages[i] = d.list_opt.get(i).page;
        }

        initShow();
        setShow();

    }

    void initShow() {
        table = new JTable();
        DefaultTableModel d = (DefaultTableModel) table.getModel();
        d.setColumnIdentifiers(name);
        table.setEnabled(false);
        add(new JScrollPane(table));

        setLayout(new GridLayout(1, 1));
        setVisible(true);
        setBounds(300, 200, 600, 300);
        setTitle("opt算法");
        addWindowListener(this);

        setDefaultCloseOperation(DISPOSE_ON_CLOSE);

    }

    void setShow() {
        opt(pages);
    }

    public void opt(int[] pages) {

        DefaultTableModel d= (DefaultTableModel) table.getModel();

        String[]str={"","","",""};

        int[] frames = new int[BlockNumber];
        for (int i = 0; i < BlockNumber; i++)   //0-frame装入并输出
        {
            frames[i] = pages[i];

            str[0]=String.valueOf(i);//编号
            str[1]=String.valueOf(pages[i]);//第几个点
            str[3]="√";
            for (int j = 0; j <= i; j++) {
                str[2]= str[2]+frames[j]+" ";
                //System.out.print(frames[j] + " ");
            }
            d.addRow(str);//添加行
            //if(i!=FRAME-1)
            str[2]="";
            //System.out.print("\n");
        }

        for (int i = BlockNumber; i < pages.length; i++)  //后续数组内容opt置换
        {
            boolean isIncluded = false;   //判断页匡内是否含有下一个内容
            int posToSwitch;  //置换位置的下标
            for (int j = 0; j < BlockNumber; j++) {
                if (frames[j] == pages[i]) {
                    isIncluded = true;//存在，不缺页
                    break;
                }
            }
            str[3]="▲";
            //缺页
            if (!isIncluded) {
                int[] nextIndexOfFrames = new int[BlockNumber];
                for (int j = 0; j < BlockNumber; j++) {
                    nextIndexOfFrames[j] = getIndexOfNum(i, frames[j]);
                }
                posToSwitch = getMaxOfArray(nextIndexOfFrames);
                frames[posToSwitch] = pages[i];
                str[3]="√";
            }

            str[0]=String.valueOf(i);
            str[1]=String.valueOf(pages[i]);
            for (int j = 0; j < BlockNumber; j++) {
                //str[2]=String.valueOf(table.getValueAt(table.getRowCount()-1,2))+frames[j];
                str[2]=str[2]+frames[j]+" ";
                //System.out.print(frames[j] + " ");
            }
            d.addRow(str);
            str[2]="";
            //System.out.print("\n");
        }
    }
    //获取最大的数组
    private int getMaxOfArray(int[] frames) {
        int i, index = 0, max;
        for (i = 0, max = frames[0]; i < BlockNumber; i++)
            if (frames[i] > max) {
                index = i;
                max = frames[i];
            }
        return index;
    }
    //获取数组的下标
    private int getIndexOfNum(int i, int toFind) {
        int num = 100;
        while (i < pages.length) {
            if (pages[i] == toFind)
                return i;    //找到
            i++;
        }
        return num;  //未找到
    }

    @Override
    public void windowOpened(WindowEvent e) {
        _D.setEnabled(false);
        //System.out.println(1);
    }

    @Override
    public void windowClosing(WindowEvent e) {
        _D.setEnabled(true);
        //System.out.println(2);
    }

    @Override
    public void windowClosed(WindowEvent e) {
        _D.setEnabled(true);
        //System.out.println(3);
    }

    @Override
    public void windowIconified(WindowEvent e) {

    }

    @Override
    public void windowDeiconified(WindowEvent e) {

    }

    @Override
    public void windowActivated(WindowEvent e) {

    }

    @Override
    public void windowDeactivated(WindowEvent e) {

    }
}
